/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.predict;

import net.dgg.dggcloud.core.idwork.KeyWorker;
import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.SessionManager;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.dao.predict.OrganizationMonthGoalDao;
import net.dgg.dggcloud.service.jrcrm.entity.predict.OrganizationMonthGoal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: OrganizationMonthGoalService
 * @Description:
 * @Author 汤宏
 * @date 2017/11/20 9:55
 */
@Service
public class OrganizationMonthGoalService extends BaseService {
    @Autowired
    private OrganizationMonthGoalDao organizationMonthGoalDao;
    @Autowired
    private KeyWorker keyWorker;
    /**
     * 根据部门ID集合查询下级业绩
     * @param orgIds 部门ID集合
     * @return
     */
    public List<Map> queryOrganizationMonthGoalByMap(String orgIds,String month){
        List<Map> monthGoal = null;
        if(StringUtils.isNotEmpty(orgIds)&&StringUtils.isNotEmpty(month)){
            Map map = new HashMap();
            map.put("orgIds",orgIds);
            map.put("month",month);
           monthGoal = organizationMonthGoalDao.queryOrganizationMonthGoalByMap(map);
        }
        return monthGoal;
    }


    public void saveOrganizationMonthGoal(Long[] orgId, Integer[] goal,String month){
        User loginUser = SessionManager.getCurrentUser();
        for (int i = 0; i < orgId.length; i++) {
            OrganizationMonthGoal organizationMonthGoal = new OrganizationMonthGoal();
            organizationMonthGoal.setMonth(month);
            organizationMonthGoal.setOrganizationId(orgId[i]);
            OrganizationMonthGoal omg = this.repository.get(organizationMonthGoal);
            if(null==omg){
                omg = new OrganizationMonthGoal();
                omg.setId(keyWorker.nextId());
                omg.setMonth(month);
                omg.setMonthGoal(goal[i]);
                omg.setOrganizationId(orgId[i]);
                omg.setCreateUser(loginUser);
                this.repository.saveOrUpdate(omg);
            }else{
                omg.setMonthGoal(goal[i]);
                omg.setUpdaterUser(loginUser);
                this.repository.saveOrUpdate(omg);
            }
        }

    }
}
